﻿namespace HiMap.Carto.Render
{
    using HiMap.Base;
    using HiMap.Carto;
    using HiMap.Style;
    using System;
    using System.Text;

    public class FeatureLayerGroupRenderClass : FeatureLayerRenderClass, IFeatureLayerGroupRender, IFeatureLayerRender, IHiMapPtr
    {
        public FeatureLayerGroupRenderClass(int Field)
        {
            base.m_InPtr = HiMap.Carto.VC.KWG_GROUPRENDER_CREATE(Field);
            base.m_Used = false;
        }

        public FeatureLayerGroupRenderClass(IFeatureLayer Layer, string FieldName)
        {
            IFeatureClass class2 = (IFeatureClass) Layer.Class;
            int nfiled = -1;
            for (int i = 0; i < class2.Fields.FieldCount; i++)
            {
                if (class2.Fields.GetField(i).Name.ToUpper() == FieldName.ToUpper())
                {
                    nfiled = i;
                    break;
                }
            }
            if (nfiled < 0)
            {
                base.m_InPtr = 0;
                base.m_Used = false;
            }
            else
            {
                base.m_InPtr = HiMap.Carto.VC.KWG_GROUPRENDER_CREATE(nfiled);
                base.m_Used = false;
            }
        }

        public FeatureLayerGroupRenderClass(int InPtr, bool Used)
        {
            base.m_InPtr = InPtr;
            base.m_Used = Used;
        }

        public bool deleteGroupValue(string GroupValue)
        {
            return HiMap.Carto.VC.KWG_GROUPRENDER_DELETE_STYLE(base.m_InPtr, new StringBuilder(GroupValue));
        }

        public string GetGroupValue(int nValue)
        {
            if ((nValue < 0) || (nValue >= this.GroupValueCount))
            {
                return "";
            }
            StringBuilder builder = new StringBuilder(0x100);
            HiMap.Carto.VC.KWG_GROUPRENDER_GET_VALUE(base.m_InPtr, nValue, builder);
            return builder.ToString();
        }

        public int GetGroupValueIndex(string ValueString)
        {
            return HiMap.Carto.VC.KWG_GROUPRENDER_GET_INDEX(base.m_InPtr, new StringBuilder(ValueString));
        }

        void IFeatureLayerGroupRender.AddGroupValue(StringBuilder GroupValue)
        {
            int num = HiMap.Carto.VC.KWG_STYLE_CREATE();
            HiMap.Carto.VC.KWG_GROUPRENDER_ADD(base.m_InPtr, GroupValue, num);
        }

        IPointStyle IFeatureLayerGroupRender.GetPointStyle(StringBuilder GroupValue)
        {
            int nstyle = HiMap.Carto.VC.KWG_GROUPRENDER_GET_INDEX(base.m_InPtr, GroupValue);
            if (nstyle < 0)
            {
                return null;
            }
            return new PointStyleClass(HiMap.Carto.VC.KWG_STYLE_GET_POINTSTYLE(HiMap.Carto.VC.KWG_GROUPRENDER_GET_STYLE(base.m_InPtr, nstyle)), true);
        }

        IPolygonStyle IFeatureLayerGroupRender.GetPolygonStyle(StringBuilder GroupValue)
        {
            int nstyle = HiMap.Carto.VC.KWG_GROUPRENDER_GET_INDEX(base.m_InPtr, GroupValue);
            if (nstyle < 0)
            {
                return null;
            }
            return new PolygonStyleClass(HiMap.Carto.VC.KWG_GROUPRENDER_GET_STYLE(base.m_InPtr, nstyle), true);
        }

        IPolylineStyle IFeatureLayerGroupRender.GetPolylineStyle(StringBuilder GroupValue)
        {
            int nstyle = HiMap.Carto.VC.KWG_GROUPRENDER_GET_INDEX(base.m_InPtr, GroupValue);
            if (nstyle < 0)
            {
                return null;
            }
            return new PolylineStyleClass(HiMap.Carto.VC.KWG_STYLE_GET_LINESTYLE(HiMap.Carto.VC.KWG_GROUPRENDER_GET_STYLE(base.m_InPtr, nstyle)), true);
        }

        bool IFeatureLayerGroupRender.SetPointStyle(StringBuilder GroupValue, IPointStyle PointStyle)
        {
            int nstyle = HiMap.Carto.VC.KWG_GROUPRENDER_GET_INDEX(base.m_InPtr, GroupValue);
            if (nstyle < 0)
            {
                return false;
            }
            if (PointStyle.Used)
            {
                return false;
            }
            HiMap.Carto.VC.KWG_STYLE_SET_POINTSTYLE(HiMap.Carto.VC.KWG_GROUPRENDER_GET_STYLE(base.m_InPtr, nstyle), PointStyle.InPtr);
            PointStyle.Used = true;
            return true;
        }

        bool IFeatureLayerGroupRender.SetPolylineStyle(StringBuilder GroupValue, IPolylineStyle PolylineStyle)
        {
            int nstyle = HiMap.Carto.VC.KWG_GROUPRENDER_GET_INDEX(base.m_InPtr, GroupValue);
            if (nstyle < 0)
            {
                return false;
            }
            if (PolylineStyle.Used)
            {
                return false;
            }
            HiMap.Carto.VC.KWG_STYLE_SET_LINESTYLE(HiMap.Carto.VC.KWG_GROUPRENDER_GET_STYLE(base.m_InPtr, nstyle), PolylineStyle.InPtr);
            PolylineStyle.Used = true;
            return true;
        }

        public int GroupValueCount
        {
            get
            {
                return HiMap.Carto.VC.KWG_GROUPRENDER_GET_COUNT(base.m_InPtr);
            }
        }

        IPointStyle IFeatureLayerGroupRender.DefaultPointStyle
        {
            get
            {
                return new PointStyleClass(HiMap.Carto.VC.KWG_STYLE_GET_POINTSTYLE(HiMap.Carto.VC.KWG_GROUPRENDER_GET_STYLE_BASE(base.m_InPtr)), true);
            }
            set
            {
                if (!value.Used)
                {
                    HiMap.Carto.VC.KWG_STYLE_SET_POINTSTYLE(HiMap.Carto.VC.KWG_GROUPRENDER_GET_STYLE_BASE(base.m_InPtr), value.InPtr);
                    value.Used = true;
                }
            }
        }

        IPolygonStyle IFeatureLayerGroupRender.DefaultPolygonStyle
        {
            get
            {
                return new PolygonStyleClass(HiMap.Carto.VC.KWG_GROUPRENDER_GET_STYLE_BASE(base.m_InPtr), true);
            }
        }

        IPolylineStyle IFeatureLayerGroupRender.DefaultPolylineStyle
        {
            get
            {
                return new PolylineStyleClass(HiMap.Carto.VC.KWG_STYLE_GET_LINESTYLE(HiMap.Carto.VC.KWG_GROUPRENDER_GET_STYLE_BASE(base.m_InPtr)), true);
            }
            set
            {
                if (!value.Used)
                {
                    HiMap.Carto.VC.KWG_STYLE_SET_LINESTYLE(HiMap.Carto.VC.KWG_GROUPRENDER_GET_STYLE_BASE(base.m_InPtr), value.InPtr);
                    value.Used = true;
                }
            }
        }

        int IFeatureLayerGroupRender.FieldIndex
        {
            get
            {
                return HiMap.Carto.VC.KWG_GROUPRENDER_GET_FIELD(base.m_InPtr);
            }
        }
    }
}

